import get from '@dfeidao/atom-web/local/get';
// 重新渲染树形图
import remove from '@dfeidao/atom-web/local/remove';
import set from '@dfeidao/atom-web/local/set';
import fire from '@dfeidao/atom-web/msg/fire';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import confirm, { ConfirmResult } from '@dfeidao/atom-web/msgbox/confirm';
import render from '@dfeidao/atom-web/render/render';
import s_get from '@dfeidao/atom-web/storage/get';
import Tree from '@dfeidao/fd-w000001';
import { IFeidaoAiBrowserComponent } from '@dfeidao/web/interfaces';
import P01 from './P01';
export default async function a004(fd: IFeidaoAiBrowserComponent, args: Event) {

	set(fd, 'arr', true);
	const n = args;
	let _no = get(fd, 'n');
	set(fd, 'no1', args);

	const productid = await s_get('productid');
	set(fd, 'client_point_id', args);
	const client_type = fd.data.node.querySelector<HTMLSelectElement>('[data-feidao-id="client_style"]').value;
	let formgroup_no;

	const v = fd.data.node.querySelector<Tree>('#widget').get_checked();

	let _checked: string[] = get(fd, 'checked');
	if (get(fd, 'checked1')) {
		_checked = [];
		remove(fd, 'checked1');
	} else {
		_checked = get(fd, 'checked');
	}
	if (!get(fd, 'menu_no')) {
	} else {
		_checked = get(fd, 'menu_no');
		remove(fd, 'menu_no');
	}


	if (get(fd, 'checked2')) {
		_checked = [];
		remove(fd, 'checked2');
	} else {
		if (_checked) {
			const arr = new Array();

			v.forEach((item) => {
				arr.push(item.id);
			});

			if (!(_checked.sort().toString() === arr.sort().toString())) {
				set(fd, 'arr', false);
				// fd.data.node.querySelector<HTMLDivElement>('.tankuang').classList.remove('display-n');
				// fd.data.node.querySelector<HTMLSpanElement>('#tishi').innerHTML = '数据已经改变！是否保存？';
				const flag = await confirm('选中数据已改变，是否保存?', '提示', '确认', '取消', '#694f96');
				switch (flag) {
					case ConfirmResult.YES:
						if (flag) {

							fire('zj-000002', 'ec-004', _no, n, _checked);
							// console.log(_no, n, _checked);
						} else {
							formgroup_no = n;
						}
						break;
					case 'no':
						break;
					default:    // cancel
						break;
				}
			}
		}
	}
	set(fd, 'n', args);
	const art = get(fd, 'arr');
	if (art) {
		_no = args;
		formgroup_no = _no;
	} else {
		formgroup_no = args;
	}
	// console.log(formgroup_no, args[0], 44444)
	const res = await nodejs<Array<{ menu_no: string, menu_name: string, parent_no: string, _id: string, checked: boolean }>>('functional-permissions/zj-000002/s003', { productid, formgroup_no, client_type });
	// console.log(_no, formgroup_no, args[0], art);
	if (res.length === 0) {
	} else {
		const cdata = res.map((d) => {
			return { id: d.menu_no, text: d.menu_name, p_id: d.parent_no, _id: d._id };
		});
		render(fd, JSON.stringify(cdata), P01, 'P01', 'inner');
	}
	const checked = new Array();
	res.forEach((item) => {
		if (item.checked === true) {
			checked.push(item.menu_no);
		}
	});
	set(fd, 'checked', checked);
	const widget: Tree = document.querySelector('#widget');
	widget.check_node(checked);
	// console.log(checked)

}

